<template>
	<MenuPage 
    :columns="reportsCol" 
	:data="archiveList"
    :page.sync="pageIndex"
    :size.sync="pageSize"
    :total="total" 
    @queryFun="getArchivePage">
	<template slot="search">
		<Input v-model.trim="content" clearable placeholder="内容检索" style="width: 180px;margin-left: 15px" @on-change="changePage(1)" />
		<Input v-model.trim="searchUserName" clearable placeholder="归档用户名" style="width: 180px;margin-left: 15px" @on-change="changePage(1)" />
		<Input v-model.trim="searchName" clearable placeholder="归档报告名" style="width: 180px;margin-left: 15px" @on-change="changePage(1)" />
		<Select filterable v-model="searchType" placeholder="选择类型" @on-change="changePage(1)" style="width: 180px;margin-left: 15px">
			<Option :value="1">考试</Option>
			<Option :value="2">演练</Option>
			<Option :value="3">竞赛</Option>
			<Option :value="4">个人综合</Option>
			<Option :value="''">全部</Option>
		</Select>
		<FormDatepicker 
				search
                v-model="searchTime"
                @on-change="changePage(1)"></FormDatepicker>
		<Button class="header-btn" type="primary" ghost style="height: 32px;margin-left: 5px;" @click="refreshList()"><i class="el-icon-refresh-right" /></Button>
	</template>
</MenuPage>
</template>
<script>
import encryption from "@/libs/encryption.js";
import { debounce } from '@/libs/utils'
import {getArchivePage,getArchiveDeatil,delArchive} from '@/api/experimenterTrack'
import FormDatepicker from '@/components/common/form-datepicker'
export default {
  components: {
	FormDatepicker
  },
  data() {
	  let September = sessionStorage.getItem("November");
    	September = JSON.parse(encryption.SelfUseDecrypt(September));
    return {
		searchType:'',
		searchUserName:'',
		searchTime:[],
		content:'',
		searchName:'',
		userName:September.userName,
		showDefText: "加载中",
        showDefImg: true,
        reportsName:'',
        reportsUsername:'',
        reportsType:'',
        pageIndex:1,
        pageSize:10,
        archiveList:[],
        total:0,
        reportsCol: [
			        // {
			        //     title: '排行',
			        //     width: 60,
					// 	key: 'rank',
			        //     align: 'center',
			        //     render:(h,params) => {
			        //   		return h('span',(params.row._index+1))
			        //     }
			        // },
					{
						title: '归档报告名',
						key: 'name',
						align: 'center',
						tooltip: true,
						placement: 'top-left',
						width: 300,
					}, 
					{
						title: '类型',
						key: 'type',
						align: 'center',
						tooltip: true,
						width: 150,
						render:(h,params)=>{
                        return h('Tag',{
                            props:{
                            }
                        },this.isType(params.row.type))
                    	}
					}, 
					{
						title: '归档人',
						key: 'userName',
						align: 'center',
						tooltip: true,
						width: 150,
					},
					{
						title: '归档时间',
						key: 'createTime',
						align: 'center',
						tooltip: true,
						width: 200,
					},
					{
						title: '报告',
						align: 'center',
						key: 'totalScore2',
						tooltip: true,
						width: 100,
						render:(h,params)=>{
              return h('Button',{
              props:{
                type:'info',
                size:'small',
              },
              style:{
                'font-size':'12px'
              },
              on:{
                click:()=>{
                  this.gotoReport(params.row)
                }
              }
                },'查看报告')
						}
					},
					{
						title: '下载',
						align: 'center',
						key: 'totalScore3',
						tooltip: true,
						width: 100,
						render:(h,params)=>{


              let downName
              let  filePath= params.row.filePath+''
              let fArr = filePath.split('/')
              if(fArr && fArr.length>0){
                let filename = fArr[fArr.length-1]
                let fnArr = filename.split('.')
                let type = fnArr[fnArr.length-1]
                downName = params.row.name +'.' + type
              }
              
              return h('a',{
                  attrs:{
                    download:downName,
                    href:location.origin+'/api/minio/download?path='+params.row.filePath
                  }
              },'下载报告')
              
              
              
              
                        }
					},
					{
                        title: '操作',
                        width: 100,
                        key:'action',
                        placement:'top-right',
						opList:[
							{
								txt:'删除',
								type:'delete',
								title:'确定要删除吗',
								tooltip:true,
								disabled:(item) => {
									return item.userName!=this.userName
								},
								on:{
									click:(data,e) => {
										this.deleteArchive(data)
									}
								}
							}
						],
						// render: (h, params) => {
						// 	return h('div', [
						// 		 h('Poptip', {
                        //             props: {
                        //                 confirm: true,
                        //                 'ok-text':"确定",
                        //                 'cancel-text':"取消",
                        //                 type: 'error',
                        //                 size: 'large',
                        //                 title: '确定要删除吗',
                        //                 transfer:true
                        //             },
                        //             attrs:{
                        //                 title: '删除'
                        //             },
                        //             style:{
                        //             },
                        //             on: {
                        //                 'on-ok': () => {
						// 					this.deleteArchive(params.row)
                        //                 }
                        //             }
                        //         }, [
                        //             h('Button', {
                        //                 props: {
                        //                     type: 'primary',
                        //                     size: 'small',
                        //                     disabled:params.row.userName!=this.userName
                        //                 },
                        //                 style:{
                        //                     border:'none',
                        //                     background:'none',
                        //                     boxShadow:'none'
                        //                 }
                        //             }, [
                        //                 h('img', {
                        //                     attrs: {
                        //                         src: params.row.userName==this.userName ? require('../../assets/project/delNote-h.png'):require('../../assets/project/delNote-d.png')
                        //                     },
                        //                     style: {
                        //                         cursor: 'pointer',
                        //                         'vertical-align': 'middle',
                        //                         display:'inline-block',
                        //                         marginBottom:'4px',
                        //                     },
                        //                 }),
                        //                 h('span',{
                        //                     style: {
                        //                         cursor: 'pointer',
                        //                         color:'#2d8cf0'
                        //                     },
                        //                 })
                        //             ])
                        //         ]),
						// 	])
						// }
					}
				],
    };
  },
	computed: {
	
	},
	filters: {

	},
	methods: {
    
    
		refreshList(){
			this.pageIndex=1
			this.searchType='',
			this.searchUserName='',
			this.content='',
			this.searchName='',
			this.searchTime = [],
			this.getArchivePage()
		},
		deleteArchive(row){
			delArchive(row.id).then(res=>{
				if(res.code==1){
					this.$Message.success('删除成功')
					this.getArchivePage()
				}else{
					this.$Message.warning(res.code)
				}
			})
		},
		isType(val){
                if(val==1){
					return '考试报告归档'
				}else if(val==2){
					return '演练报告归档'
				}else if(val==3){
					return '竞赛报告归档'
				}else{
					return '个人综合报告归档'
				}
                
            },
		// setStyle:debounce(function() {
		// 	window.fullHeight = document.documentElement.clientHeight;
		// 	window.fullWidth = document.documentElement.clientWidth;
		// 	this.fullHeight = window.fullHeight;
		// 	this.fullWidth = window.fullWidth;
		// 	this.setTableHei = this.fullHeight - 197
		// 	this.pageSize = window.innerHeight>1200?20:10 // pageSize 如果不是10，和20 会有问题。之前算出来那种方式不展示分页每页多少条。
		// 	if(this.total > this.pageSize){
		// 		let maxPage = Math.ceil(this.total/this.pageSize);
		// 		if(maxPage < this.pageIndex){
		// 			this.pageIndex = maxPage;
		// 		}
		// 	}
		// 	this.getArchivePage();
		// },500),

    download(row,fileUrl){
      //                 download:params.row.name+'.pdf',
      // href:'/api/minio/download?path='+params.row.filePath

      var xhr = new XMLHttpRequest();
      xhr.open("GET", location.origin+fileUrl, true);
      xhr.responseType = 'blob';
      xhr.onload=function(e) {
        var url = URL.createObjectURL(e)//传入返回的bobl对象
        var a = document.createElement('a');
        a.href = url
        a.download = row.name+'.pdf';//修改的文件名
        a.click()
      }
      xhr.send();
      
      
    },
    
		gotoReport(row){
			if(row.type==1){
				const { href } = this.$router.resolve({
					name:'archiveExamReports',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==3&&(row.drillType==8||row.drillType==1||row.drillType==9)){
				const { href } = this.$router.resolve({
					name:'archiveAdcReportNew',
					query:{
						id:row.id,
						type:row.drillType
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==3&&row.drillType==2){
				const { href } = this.$router.resolve({
					name:'archiveCtfReport',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==3&&row.drillType==3){
				const { href } = this.$router.resolve({
					name:'archiveKcReport',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==2&&row.drillType==0){
				const { href } = this.$router.resolve({
					name:'archiveAwdReportNew',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==2&&row.drillType==4){
				const { href } = this.$router.resolve({
					name:'archiveStReportNew',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==2&&row.drillType==7){
				const { href } = this.$router.resolve({
					name:'archiveZhReportNew',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==2&&row.drillType==5){
				const { href } = this.$router.resolve({
					name:'archiveHwReportNew',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}else if(row.type==4){
				const { href } = this.$router.resolve({
					name:'archiveStatisticalReport',
					query:{
						id:row.id,
					}
				});
				window.openTrain(href, '_blank')
			}
			// console.log(row,11111111111111);
		},
		changePage(val){
			this.pageIndex=val
			this.getArchivePage()
		},
		getArchivePage:debounce(function() {
			let params={
				pageIndex:this.pageIndex,
				pageSize:this.pageSize,
				userName:this.searchUserName,
				content:this.content,
				name:this.searchName,
			}
			if(this.searchType!=''){
				params.type=this.searchType
			}else{
				delete  params.type
			}
			if(this.searchTime && this.searchTime.length > 0 && this.searchTime[0]){
				params.findStartTime = this.searchTime[0]
				params.findEndTime = this.searchTime[1]
			}
			console.log(params)
			getArchivePage(params).then(res=>{
				if(res.code==1){
					this.archiveList=res.data.items
					this.total=res.data.total
					if(this.archiveList.length>0){
						this.showDefImg = false;
					}else{
						this.showDefImg = true;
						this.showDefText = "暂无信息"
					}
				}else{
					this.$Message.warning(res.message)
					this.showDefImg = true;
					this.showDefText = "暂无信息"
				}
			})
		},500),
	
	},
	mounted() {
		// this.setStyle()
		// window.onresize = () => {
		// 	this.setStyle()
		// }
		this.getArchivePage()
	},
	beforeDestroy(){
			window.onresize = null;
		},
	watch: {},
};
</script>

<style scoped>
</style>